
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>App列表</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
  <style>

/*    .layui-table-cell {*/
/*      font-size: 14px;*/
/*      padding: 0 5px;*/
/*      height: auto;*/
/*      overflow: visible;*/
/*      text-overflow: inherit;*/
/*      white-space: normal;*/
/*      word-break: break-all;*/
/*    }*/

  </style>
</head>
<body>

  <div class="layui-fluid">   
    <div class="layui-card">
      <div class="layui-form layui-card-header layuiadmin-card-header-auto">
        <div class="layui-form-item">

          <div class="layui-inline">
            <input type="text" name="id" placeholder="ID" autocomplete="off" class="layui-input">
          </div>
          <div class="layui-inline">
            <input type="text" name="name" placeholder="包名称" autocomplete="off" class="layui-input">
          </div>
          <div class="layui-inline">
            <input type="text" name="package_name" placeholder="包名" autocomplete="off" class="layui-input">
          </div>

          <div class="layui-inline">
                <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-search">
                    <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                </button>
            </div>

          <div class="layui-inline" style="float: right;">
            <button class="layui-btn layuiadmin-btn-role" data-type="add">添加</button>
          </div>


        </div>
      </div>
      <div class="layui-card-body">

        <script type="text/html" id="toolbarDemo">
          <div class="layui-btn-container">
            <a class="layui-btn layui-btn-sm layui-btn layui-btn-normal" href="/quick_app/examineNovels">设置审核书籍</a>
            <button class="layui-btn layui-btn-primary layui-btn-sm layui-border-orange" lay-event="pbi">批量设置屏蔽规则</button>
          </div>
        </script>

        <table id="LAY-user-back-role" lay-filter="LAY-user-back-role"></table>

        <script type="text/html" id="contactTpl">
          <span id="contactId_{{d.id}}">{{d.info.contact}}</span>
        </script>

        <script type="text/html" id="wxpaymentTpl">
          <span>{{d.info.wx_payment_id}}</span>
        </script>

        <script type="text/html" id="zfbpaymentTpl">
          <span>{{d.info.zfb_payment_id}}</span>
        </script>

        <script type="text/html" id="table-useradmin-admin">
          <a class="layui-btn layui-btn-primary layui-border-red layui-btn-xs" lay-event="shield">屏蔽规则</a>
          <a class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
          <a class="layui-btn layui-btn-primary layui-border-green layui-btn-xs" lay-event="contract" data-id="{{d.id}}">获取协议</a>

        </script>
      </div>
    </div>
  </div>



  <script src="/static/layuiadmin/layui/layui.js"></script>

  <script>
  layui.config({
    base: '/static/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index','table','dropdown'], function(){
    var $ = layui.$
    ,form = layui.form
    ,table = layui.table
    ,admin = layui.admin
    ,dropdown = layui.dropdown;

      //监听搜索
      form.on('submit(LAY-user-back-search)', function(data){
          var field = data.field;

          //执行重载
          table.reload('LAY-user-back-role', {
              where: field
            ,page: {
              curr: 1  // 将页码重置为第一页
            }
          });
      });

    table.render({
      elem: '#LAY-user-back-role'
      , url: '/quick_app/list' //模拟接口
      , response: {
        statusCode: 1 //规定成功的状态码，本来是：0，现在统一改为1
      }
      ,cellMinWidth: 180
      , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
      , defaultToolbar: false
      , page: true //开启分页
      , limit: 20
      , cols: [[
        {type: 'checkbox'}
        , {field: 'id', width: 80, title: 'ID'}
        , {field: 'name', title: '名称'}
        , {field: 'package_name', title: '包名'}
        , {
          field: 'subject_name', title: '主体', templet: function (d) {
            if (d.subject) {
              //得到当前行数据，并拼接成自定义模板
              return d.subject.subject_name
            } else {
              return '';
            }
          }
        }
        , {
          field: 'contact', title: '客服信息', templet: '#contactTpl', edit: 'text'
        }
        , {
          field: 'remarks', title: '具体描述', edit: 'text', templet: function (d) {
            if (d.info) {
              //得到当前行数据，并拼接成自定义模板
              return "<span id='remarks_" + d.id + "'>" + d.info.remarks + "</span>";
            } else {
              return "<span id='remarks_" + d.id + "'></span>";
            }
          }
        }
        , {
          field: 'wx_payment_id', title: '微信支付号', templet: '#wxpaymentTpl'
        }
        , {
          field: 'zfb_payment_id', title: '支付宝支付号', templet: '#zfbpaymentTpl'
        }
        , {field: 'create_time', title: '创建时间'}
        , {title: '操作', minWidth: 300, align: 'center', fixed: 'right', toolbar: '#table-useradmin-admin'}
      ]]
    });


    //监听单元格编辑
    table.on('edit(LAY-user-back-role)', function (obj) { //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
      console.log(obj.value); //得到修改后的值
      console.log(obj.field); //当前编辑的字段名
      if (obj.field === 'contact'){
        admin.req({
          url: '/quick_app/changeContact'
          , type: "POST"
          , data: {'ids': [obj.data.id], 'contact': obj.value}
          , done: function (res) {
            layer.msg('设置成功', {icon: 1, time: 1000}, function () {
              layer.closeAll();
              $("#contactId_"+obj.data.id).html(obj.value);
              // table.reload('LAY-user-back-role');
            });
          }, not_finish: function (res) {
            layer.msg(res.msg);
          }
        });
      }else{
        admin.req({
          url: '/quick_app/edit' + obj.field
          , type: "POST"
          , data: {'id': obj.data.id, 'remarks': obj.value}
          , done: function (res) {
            layer.msg('设置成功', {icon: 1, time: 1000}, function () {
              layer.closeAll();
              $("#remarks_"+obj.data.id).html(obj.value);
              // table.reload('LAY-user-back-role');
            });
          }, not_finish: function (res) {
            layer.msg(res.msg);
          }
        });
      }
    });

    //监听行工具事件
    table.on('tool(LAY-user-back-role)', function(obj){
      var data = obj.data;
      if(obj.event === 'shield'){
        layer.open({
          type: 2
          , maxmin: true//开启最大化最小化按钮
          , title: '屏蔽规则'
          , content: '/quick_app/blockingRules.html?id=' + obj.data.id
          , area: ['50%', '90%']
          , btn: ['确定', '取消']
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
                    ,submit = layero.find('iframe').contents().find("#LAY-user-role-submit");

            //监听提交
            iframeWindow.layui.form.on('submit(LAY-user-role-submit)', function(data){
              var field = data.field; //获取提交的字段
              //提交 Ajax 成功后，静态更新表格中的数据
              //$.ajax({});
              admin.req({
                url: '/quick_app/setBlockRule'
                , type: "POST"
                , data: field
                , done: function (res) {
                  layer.msg('设置成功', {icon: 1, time: 1000}, function () {
                    layer.close(index); //关闭弹层
                  });
                },not_finish:function (res) {
                  layer.msg(res.msg);
                }
              });
            });
            submit.trigger('click');
          }
        });
      }else if(obj.event === 'edit'){
        var tr = $(obj.tr);
        layer.open({
          type: 2
          , title: '修改App'
          ,offset: '5px'
          , content: '/quick_app/quickAppForm.html?id=' + obj.data.id
          , area: ['500px', '80%']
          , maxmin: true//开启最大化最小化按钮
          , btn: ['确定', '取消']
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
                    ,submit = layero.find('iframe').contents().find("#LAY-user-role-submit");

            //监听提交
            iframeWindow.layui.form.on('submit(LAY-user-role-submit)', function(data){
              var field = data.field; //获取提交的字段
              //提交 Ajax 成功后，静态更新表格中的数据
              //$.ajax({});
              admin.req({
                url: '/quick_app/setQuickApp'
                , type: "POST"
                , data: field
                , done: function (res) {
                  layer.msg('修改成功', {icon: 1, time: 1000}, function () {
                    table.reload('LAY-user-back-role');
                    layer.close(index); //关闭弹层
                  });
                },not_finish:function (res) {
                  layer.msg(res.msg);
                }
              });
            });
            submit.trigger('click');
          }
        });
      } else if (obj.event === 'more') {
        dropdown.render({
          elem: this
          , show: true //外部事件触发即显示
          , data: [{
            title: '展示推送配置'
            , id: 'showSendConfig'
          }, {'type': '-'}, {
            title: '华为'
            , id: 'huawei'
          }, {
            title: '小米'
            , id: 'xiaomi'
          }, {
            title: 'vivo'
            , id: 'vivo'
          }, {
            title: 'OPPO'
            , id: 'OPPO'
          }]
          , click: function (data, othis) {
            //根据 id 做出不同操作
            var elem = $(this.elem)
                    ,listId = elem.data('id'); //表格列表的预埋数据
            if (data.id === 'huawei' || data.id === 'xiaomi' || data.id === 'OPPO' || data.id === 'vivo') {
              layer.open({
                type: 2
                , title: data.title
                , content: '/quick_app/sendConfigForm.html?id=' + listId + '&mobile_brand=' + data.id
                , area: ['800px', '340px']
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                  var iframeWindow = window['layui-layer-iframe' + index]
                          , submit = layero.find('iframe').contents().find("#LAY-user-role-submit");
                  //监听提交
                  iframeWindow.layui.form.on('submit(LAY-user-role-submit)', function (data) {
                    var field = data.field; //获取提交的字段
                    //提交 Ajax 成功后，静态更新表格中的数据
                    //$.ajax({});
                    admin.req({
                      url: '/quick_app/setSendConfig'
                      , type: "POST"
                      , data: field
                      , done: function (res) {
                        layer.msg('设置成功', {icon: 1, time: 1000}, function () {
                          table.reload('LAY-user-back-role');
                          layer.close(index); //关闭弹层
                        });
                      },not_finish:function (res) {
                        layer.msg(res.msg);
                      }
                    });
                  });
                  submit.trigger('click');
                }
              });
            } else if (data.id === 'showSendConfig') {
              layer.open({
                type: 2
                , title: data.title
                , content: '/quick_app/sendConfigForm.html?id=' + listId + '&mobile_brand=show'
                , area: ['800px', '500px']
              });
            }
          }
          , align: 'right' //右对齐弹出（v2.6.8 新增）
          , style: 'box-shadow: 1px 1px 10px rgb(0 0 0 / 12%);' //设置额外样式
        });

      }else if (obj.event === 'contract') {
        admin.req({
          url: '/quick_app/getContract'
          , type: "POST"
          , data: {id: obj.data.id}
          , done: function (res) {
            $("#user").val(res.data.user);
            $("#privacy").val(res.data.privacy);
            $("#vip").val(res.data.vip);
            layer.open({
              type: 1,
              title:'链接详情',
              offset: '15px',
              area: '700px',
              skin: 'layui-layer-demo', //样式类名
              closeBtn: 0, //不显示关闭按钮
              anim: 2,
              shadeClose: true, //开启遮罩关闭
              content: $('.link-show')
            });

          },not_finish:function (res) {
            layer.msg(res.msg);
          }
        });
      }
    });

    //头工具栏事件
    table.on('toolbar(LAY-user-back-role)', function (obj) {
      var checkStatus = table.checkStatus(obj.config.id);
      switch (obj.event) {
        case 'getCheckData':
          var data = checkStatus.data;
          if (data.length === 0) {
            layer.msg('请选择App设置');
            return false;
          }
          var ids = [];
          for (var key in data) {
            ids.push(data[key]['id']);
          }
          layer.prompt({title: '输入客服信息'}, function(contact, index){
            admin.req({
              url: '/quick_app/changeContact'
              , type: "POST"
              , data: {'ids':ids,'contact':contact}
              , done: function (res) {
                layer.msg('设置成功', {icon: 1, time: 1000}, function () {
                  layer.closeAll();
                  table.reload('LAY-user-back-role');
                });
              }, not_finish: function (res) {
                layer.msg(res.msg);
              }
            });
          });
          break;
        case 'pbi':
          var data = checkStatus.data;
          if (data.length === 0) {
            layer.msg('请选择App设置');
            return false;
          }
          var ids = [];
          for (var key in data) {
            ids.push(data[key]['id']);
          }
          layer.open({
            type: 2
            , maxmin: true//开启最大化最小化按钮
            , title: '屏蔽规则'
            , content: '/quick_app/blockingRules.html?type=2&ids=' + ids
            , area: ['50%', '90%']
            , btn: ['确定', '取消']
            ,yes: function(index, layero){
              var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submit = layero.find('iframe').contents().find("#LAY-user-role-submit");

              //监听提交
              iframeWindow.layui.form.on('submit(LAY-user-role-submit)', function(data){
                var field = data.field; //获取提交的字段
                //提交 Ajax 成功后，静态更新表格中的数据
                //$.ajax({});
                admin.req({
                  url: '/quick_app/setBlockRule'
                  , type: "POST"
                  , data: field
                  , done: function (res) {
                    layer.msg('设置成功', {icon: 1, time: 1000}, function () {
                      layer.close(index); //关闭弹层
                    });
                  },not_finish:function (res) {
                    layer.msg(res.msg);
                  }
                });
              });
              submit.trigger('click');
            }
          });
          break;
      }
    });

    //事件
    var active = {
      add: function(){
        layer.open({
          type: 2
          ,title: '添加新App'
          ,content: '/quick_app/quickAppForm.html'
          ,offset: '5px'
          , area: ['500px', '80%']
          , maxmin: true//开启最大化最小化按钮
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
            ,submit = layero.find('iframe').contents().find("#LAY-user-role-submit");

            //监听提交
            iframeWindow.layui.form.on('submit(LAY-user-role-submit)', function(data){
              var field = data.field; //获取提交的字段
              //提交 Ajax 成功后，静态更新表格中的数据
              //$.ajax({});
              admin.req({
                url: '/quick_app/setQuickApp'
                , type: "POST"
                , data: field
                , done: function (res) {
                  layer.msg('添加成功', {icon: 1, time: 1000}, function () {
                    table.reload('LAY-user-back-role');
                    layer.close(index); //关闭弹层
                  });
                },not_finish:function (res) {
                  layer.msg(res.msg);
                }
              });
            });

            submit.trigger('click');
          }
        });
      },
      copy1:function () {
        $("#user").select(); // 选择对象
        document.execCommand("Copy"); // 执行浏览器复制命令
        layer.msg('复制成功', {icon: 1, time: 1000});
      },
      copy2:function () {
        $("#privacy").select(); // 选择对象
        document.execCommand("Copy"); // 执行浏览器复制命令
        layer.msg('复制成功', {icon: 1, time: 1000});
      },
      copy3:function () {
        $("#vip").select(); // 选择对象
        document.execCommand("Copy"); // 执行浏览器复制命令
        layer.msg('复制成功', {icon: 1, time: 1000});
      }
    }
    $('.layui-btn.layuiadmin-btn-role').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
  });
  </script>

  <div class="link-show" style="padding:20px;display: none;">
    <form class="layui-form layui-form-pane" action="">

      <div class="layui-form-item">
        <label class="layui-form-label">用户协议</label>
        <div class="layui-inline" style="width: 460px;">
          <input type="text" id="user" class="layui-input" value="" readonly>
        </div>
        <div class="layui-inline">
          <button type="button" class="layui-btn layuiadmin-btn-role link" data-type="copy1">复制</button>
        </div>
      </div>

      <div class="layui-form-item">
        <label class="layui-form-label">隐私政策</label>
        <div class="layui-inline" style="width: 460px;">
          <input type="text" id="privacy" class="layui-input" value="" readonly>
        </div>
        <div class="layui-inline">
          <button type="button" class="layui-btn layuiadmin-btn-role link" data-type="copy2">复制</button>
        </div>
      </div>

      <div class="layui-form-item">
        <label class="layui-form-label">vip协议</label>
        <div class="layui-inline" style="width: 460px;">
          <input type="text" id="vip" class="layui-input" value="" readonly>
        </div>
        <div class="layui-inline">
          <button type="button" class="layui-btn layuiadmin-btn-role link" data-type="copy3">复制</button>
        </div>
      </div>


    </form>

  </div>
</body>

</html>

